package cn.dsttl3.tupan.utils.baidu;

import cn.dsttl3.tupan.utils.RedirectInterceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class BaiduDownloadLink {
    /**
     * 百度网盘dlink转直链
     * @param dlink
     * @return 直链
     */
    public String load(String dlink, String token){
        try {
            OkHttpClient client = new OkHttpClient.Builder().followRedirects(false)// 禁制OkHttp的重定向操作，自己处理重定向
                    .addInterceptor(new RedirectInterceptor()).build();
            Request request = new Request.Builder()
                    .header("User-Agent","pan.baidu.com")
                    .url(dlink + "&access_token=" + token)
                    .build();
            Response response = client.newCall(request).execute();
            return response.header("location");//获取重定向下载链接
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
